#pragma once
#include <string>
#include <iostream>
#include "hash.hpp"

using namespace std;

namespace my_unordered_set
{
	// 指名命名空间
	using namespace my_hash;

	template <typename K>
	struct HashFun
	{
		size_t operator()(const K& key)
		{
			return key;
		}
	};

	template<>
	struct HashFun<string>
	{
		size_t operator()(const string& key)
		{
			size_t hash = 0;
			for (auto ch : key)
			{
				hash = hash * 131 + ch;
			}
			return hash;
		}
	};

	template <typename K, typename Val>
	struct KeyOfVal
	{
		const K& operator()(const Val& kv)
		{
			return kv;
		}
	};
	// 由外面传入 key的哈希转换函数
	template <typename K, typename HashF = HashFun<K>>
	class unordered_set
	{
	public:
		typedef Hash<K, K, KeyOfVal<K, K>, HashF> Hash_;
		typedef typename Hash_::const_iterator iterator;
		// iterator 实际是const_iterator 通过这样的方法来保证set中的key不可以改变
		// 这就有个问题  _hash.begin(); 返回的是iterator  而begin 返回的 const_iterator
		// 所以迭代器的构造函数中需要存在用 iterator构造const_iterator的构造函数！
		typedef typename Hash_::const_iterator const_iterator;


		std::pair<iterator, bool> insert(const K& k)
		{
			return _hash.insert(k);
		}
		iterator find(const K& key)
		{
			return _hash.find(key);
		}
		// 返回删除节点的下一个节点
		iterator erase(const_iterator pos)
		{
			return _hash.erase(pos);
		}
		iterator begin()
		{
			return _hash.begin();
		}
		iterator end()
		{
			return _hash.end();

		}
		const_iterator begin() const
		{
			return _hash.begin();
		}
		const_iterator end() const
		{
			return _hash.end();
		}



	private:
		Hash_ _hash;
	};

	void test_set()
	{
		int a[] = { 2,3,33,23,4,14,24,34 };
		unordered_set<int> s;
		for (auto e : a)
		{
			s.insert(e);
		}
		unordered_set<int>::iterator it = s.begin();
		//*it = 1;
		cout << *it << endl;


	}
	class Date
	{
		// 使用友元，访问内部类私有属性
		friend struct HashDate;
	public:
		Date(int year = 1900, int month = 1, int day = 1)
			: _year(year)
			, _month(month)
			, _day(day)
		{}
		bool operator<(const Date& d)const
		{
			return (_year < d._year) ||
				(_year == d._year && _month < d._month) ||
				(_year == d._year && _month == d._month && _day < d._day);
		}
		bool operator>(const Date& d)const
		{
			return (_year > d._year) ||
				(_year == d._year && _month > d._month) ||
				(_year == d._year && _month == d._month && _day > d._day);
		}
		bool operator==(const Date& d)const
		{
			return _year == d._year && _month == d._month && _day == d._day;
		}
		bool operator!=(const Date& d)const
		{
			return !(*this == d);
		}
		friend ostream& operator<<(ostream& _cout, const Date& d)
		{
			_cout << d._year << "-" << d._month << "-" << d._day;
			return _cout;
		}
	private:
		int _year;
		int _month;
		int _day;
	};
	struct HashDate
	{
		size_t operator()(const Date& d)
		{
			size_t hash = 0;
			hash += d._year;
			hash *= 31;
			hash += d._month;
			hash *= 31;
			hash += d._day;
			hash *= 31;

			return hash;
		}
	};
	void test_set2()
	{
		Date d1(2013, 12, 1);
		Date d2(2013, 11, 2);
		Date d3(2013, 1, 3);
		Date d4(2013, 2, 4);
		Date d5(2013, 5, 5);
		Date d6(2013, 7, 6);
		vector<Date> vd({ d1,d2,d3,d4,d5,d6 });
		unordered_set<Date, HashDate> s;
		for (auto& d : vd)
		{
			s.insert(d);
		}
		unordered_set<Date, HashDate>::iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << endl;
			++it;
		}

	}
}